package com.sicnu.estatemanagement.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sicnu.estatemanagement.dao.HomeMapper;
import com.sicnu.estatemanagement.domain.House;
import com.sicnu.estatemanagement.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.util.List;
import java.util.Map;

@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    private HomeMapper homeMapper;

    @Override
    public List<House> findAll() {
        List<House> houseList = homeMapper.selectAll();
        return houseList;
    }

    @Override
    public Page<House> search(Map searchMap) {
        //通用Mapper多条件搜索，标准写法
        Example example = new Example(House.class);//指定查询的表tb_community
        //1.初始化分页条件
        int pageNum = 1;
        int pageSize = 2;
        if(searchMap != null){
            Example.Criteria criteria = example.createCriteria();//创建查询条件
            //时间区间
            if(StringUtil.isNotEmpty((String) searchMap.get("startTime"))){
                criteria.andGreaterThanOrEqualTo("createTime",searchMap.get("startTime"));
            }
            if(StringUtil.isNotEmpty((String) searchMap.get("endTime"))){
                criteria.andLessThanOrEqualTo("createTime",searchMap.get("endTime"));
                criteria.andLessThanOrEqualTo("createTime",searchMap.get("endTime"));
            }
            //名称模糊搜索
            if(StringUtil.isNotEmpty((String) searchMap.get("name"))){
                criteria.andLike("name", "%"+(String) searchMap.get("name")+"%");
            }
            if((Integer) searchMap.get("pageNum") !=null){
                pageNum = (Integer) searchMap.get("pageNum");
            }
            if((Integer) searchMap.get("pageSize") !=null){
                pageSize = (Integer) searchMap.get("pageSize");
            }
        }
        PageHelper.startPage(pageNum,pageSize);//使用PageHelper插件完成分页
        Page<House> houseList = (Page<House>) homeMapper.selectByExample(example);
        return houseList;
    }

    @Override
    public Boolean add(House house) {
        int row = homeMapper.insert(house);
        if(row>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public House findById(Integer id) {
        return homeMapper.selectByPrimaryKey(id);
    }

    @Override
    public Boolean update(House house) {
        int row = homeMapper.updateByPrimaryKeySelective(house);
        if(row>0){
            return true;
        }else{
            return false;
        }
    }

//    @Override
//    public Boolean updateStatus(String status, Integer id) {
//        House House = new House();
//        House.setId(id);
//        House.setStatus(status);
//        int row = homeMapper.updateByPrimaryKeySelective(House);
//        if(row>0){
//            return true;
//        }else{
//            return false;
//        }
//    }

    @Override
    public Boolean del(List<Integer> ids) {
        for (Integer id:ids) {
            homeMapper.deleteByPrimaryKey(id);
        }
        return true;
    }

}
